mui.init();

var send = document.getElementById('send');
var emotions = document.getElementById('emotions');
var more = document.getElementById('more');
var speech = document.getElementById('speech');
var msgInput = document.getElementById('msg-input');
var msgValue = msgInput.value;

document.getElementById('msg-input').addEventListener('input',function() {
	msgInput = document.getElementById('msg-input');
	msgValue = msgInput.value;
	if (this.value.trim().length > 0) {
		send.style.display='block';
		emotions.style.display='none';
		more.style.display='none';
	} else {
		send.style.display='';
		emotions.style.display='';
		more.style.display='';
	}
});

// 语音识别
document.getElementById('speech').addEventListener('tap', function() {
	msgInput = document.getElementById('msg-input');
	msgValue = msgInput.value;
	// 监听plusready事件  
	document.addEventListener( "plusready", function(){
		// 扩展API加载完毕，现在可以正常调用扩展API
		// ...
	}, false );
	if(plus.os.name=='Android'&&navigator.userAgent.indexOf('StreamApp')>0){
		plus.nativeUI.toast('当前环境暂不支持语音识别插件');
		return;
	}
	var options = {};
	options.engine = 'iFly';
	//options.punctuation = false;	// 是否需要标点符号 
	msgValue = "";
	confirm( "开始语音识别：" );
	plus.speech.startRecognize( options, function ( s ) {
//		outLine( s );
		msgInput.value += s;
		msgValue += s;
		send.style.display='block';
		emotions.style.display='none';
		more.style.display='none';
//		alert(msgValue);
	}, function ( e ) {
		alert( "语音识别失败："+e.message );
	});
	function stopRecognize(){
		plus.speech.stopRecognize();
	}
	setTimeout( stopRecognize, 10000 );
});

document.getElementById('send').addEventListener('tap',function() {
//	alert(document.getElementById('msg-input').value);
	var willSendMsg = '<div class="chat-box chat-box-right mui-content-padded">'+
		              '  		<img class="chat-avatar" src="../../images/wei2.jpg" />'+
		              '  		<div class="chat-content">'+
		              '  			<div class="chat-content-inner">'+
									msgValue+
		              '  			</div>'+
		              '  			<div class="chat-content-arrow"></div>'+
		              '  		</div>'+
		              '  		<div class="clear-float"></div>'+
		              '</div>';
	var newDemo = document.createElement('div');
	newDemo.innerHTML = willSendMsg;
	var msgList = document.querySelector('.chat-list');
	// 把拼接好的气泡html追加到消息列表末尾
	msgList.appendChild(newDemo);
	
	// 清空文本输入框对象
	msgInput.value = '';
	// 显示表情和更多按钮，隐藏发送按钮
	send.style.display='';
	emotions.style.display='';
	more.style.display='';
	
	// 显示底部消息
	msgList.scrollTop = msgList.scrollHeight-msgList.offsetHeight;
	
	// Tom 用自己的名字作为 clientId，获取 IMClient 对象实例
	realtime.createIMClient('Tom').then(function(tom) {
	  // 创建与Jerry之间的对话
	  return tom.createConversation({
	    members: ['Jerry'],
	    name: 'Tom & Jerry',
	  });
	}).then(function(conversation) {
	  // 发送消息
	  return conversation.send(new AV.TextMessage(msgValue));
	}).then(function(message) {
	  console.log('Tom & Jerry', '发送成功！');
	}).catch(console.error);
});

// 初始化实时聊天
var Realtime = AV.Realtime;
var TextMessage = AV.TextMessage;
// 初始化
var realtime = new Realtime({
  appId: '169hzeIJ8pkXAStYXPFwaFBs-gzGzoHsz',
  region: 'cn', //美国节点为 "us"
});

// Tom 接收消息
realtime.createIMClient('Tom').then(function(tom) {
  tom.on('message', function(message, conversation) {
    console.log('Message received: ' + message.text);
    var willSendMsg = '<div class="chat-box chat-box-left mui-content-padded">'+
		              '  		<img class="chat-avatar" src="../../images/wei1.jpg" />'+
		              '  		<div class="chat-content">'+
		              '  			<div class="chat-content-inner">'+
									message.text+
		              '  			</div>'+
		              '  			<div class="chat-content-arrow"></div>'+
		              '  		</div>'+
		              '  		<div class="clear-float"></div>'+
		              '</div>';
	var newDemo = document.createElement('div');
	newDemo.innerHTML = willSendMsg;
	var msgList = document.querySelector('.chat-list');
	// 把拼接好的气泡html追加到消息列表末尾
	msgList.appendChild(newDemo);
	// 显示底部消息
	msgList.scrollTop = msgList.scrollHeight-msgList.offsetHeight;
  });
}).catch(console.error);